package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.OrderDao;
import dao.RowMapper;
import entity.Address;
import entity.Area;
import entity.Customer;
import entity.Driver;
import entity.DriverState;
import entity.Order;
import entity.OrderState;
import entity.Street;

public class OrderDaoImpl implements OrderDao {
	private JDBCTemplate jdbc=new JDBCTemplate();
	
	private class RowMapperImpl implements RowMapper<Order>{

		@Override
		public Order rowMapper(ResultSet rs) throws SQLException {
			Order order = new Order();
			order.setOrderDate(rs.getDate("orderDate"));
			order.setOrderId(rs.getInt("orderId"));
			order.setOrderParentId(rs.getString("orderParentId"));
			order.setTotalPrice(rs.getDouble("totalPrice"));
			
			OrderState orderState = new OrderState();
			orderState.setOrderStateId(rs.getInt("orderStateId"));
			orderState.setOrderStateName(rs.getString("orderStateName"));
			order.setOrderState(orderState);
			
			Driver driver = new Driver();
			driver.setDriverId(rs.getInt("driverId"));
			driver.setPassword(rs.getString("driPwd"));
			driver.setPhone(rs.getString("driPhone"));
			driver.setRealName(rs.getString("realName"));
			driver.setUserName(rs.getString("driUserName"));
			
			DriverState driverState = new DriverState();
			driverState.setDsId(rs.getInt("drStateId"));
			driverState.setStateName(rs.getString("stateName"));
			driver.setDriverState(driverState);
			order.setDriver(driver);
			
			Address address = new Address();
			address.setAddrid(rs.getInt("addressId"));
			address.setDetailAddress(rs.getString("detailAddress"));
			
			Customer customer = new Customer();
			customer.setCusId(rs.getInt("custId"));
			customer.setEmail(rs.getString("email"));
			customer.setPassword(rs.getString("custPwd"));
			customer.setPhone(rs.getString("custPhone"));
			customer.setRegisterDate(rs.getDate("registerDate"));
			customer.setUserName(rs.getString("custUserName"));
			address.setCustomer(customer);
			
			Street street = new Street();
			street.setStrId(rs.getInt("streId"));
			street.setStreetName(rs.getString("streetName"));
			
			Area area = new Area();
			area.setAreaId(rs.getInt("arId"));
			area.setAreaName(rs.getString("areaName"));
			street.setArea(area);
			address.setStreet(street);
			address.setCompleteAddress(jdbc.queryCompleteAddress(address));
			order.setAddress(address);
			return order;
		}
		
	}
	
	@Override
	public void addOrder(Order order) {
		String sql = "INSERT INTO `order`(orderParentId,orderDate,orderStateId,driverId,addressId,totalPrice)"+ 
					"VALUES(?,?,?,?,?,?)";
		jdbc.CUDObject(sql, order.getOrderParentId(),order.getOrderDate(),
						order.getOrderState().getOrderStateId(),order.getDriver().getDriverId(),
						order.getAddress().getAddrid(),order.getTotalPrice());
	}

	@Override
	public void updateOrder(Order order) {
		String sql ="update `order` set orderParentId=?,orderDate=?,orderStateId=?,driverId=?,addressId=?,totalPrice=? where orderId=?";
		jdbc.CUDObject(sql, order.getOrderParentId(),order.getOrderDate(),
				order.getOrderState().getOrderStateId(),order.getDriver().getDriverId(),
				order.getAddress().getAddrid(),order.getTotalPrice(),order.getOrderId());
	}

	
	@Override
	public List<Order> queryAllOrder() {
		return query(null, null);
	}

	@Override
	public List<Order> queryOrderByDriverId(int driverId) {
		return query("driver", driverId);
	}

	@Override
	public List<Order> queryOrderByCusId(int cusId) {
		return query("customer", cusId);
	}

	@Override
	public List<Order> queryOrderByOrderStateId(int orderStateId) {
		return query("orderState", orderStateId);
	}
	
	
	private List<Order> query(String type,Integer id){
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT"+ 
				" `order`.*,orderStateName,"+
				" driver.userName AS driUserName,realName,driver.`password` AS driPwd,driver.phone AS driPhone,"+
				" driver.`driverStateId` AS drStateId,stateName,"+
				" detailAddress,"+
				" address.`cusId` AS custId,email,customer.`password` AS custPwd,customer.phone AS custPhone,"+
				" registerDate,customer.userName AS custUserName,"+
				" address.`strId` AS streId,street.`streetName`,"+
				" street.`areaId` AS arId,areaName"+
				" FROM `order`"+
				" INNER JOIN orderState ON orderState.`orderStateId`=`order`.`orderStateId`"+
				" INNER JOIN driver ON driver.`driverId`=`order`.`driverId`"+
				" INNER JOIN driverState ON driverState.`dsId`=driver.`driverStateId`"+
				" INNER JOIN address ON address.`addrId`=`order`.`addressId`"+
				" INNER JOIN customer ON customer.`cusId`=address.`cusId`"+
				" INNER JOIN street ON street.`strId`=address.`strId`"+
				" INNER JOIN `area` ON street.`areaId` = `area`.`areaId`"+
				" WHERE 1=1";
		if(id!=null){
			if("driver".equals(type)){
				sql+=" AND driver.`driverId`=?";
			}else if("customer".equals(type)){
				sql+=" AND customer.`cusId`=?";
			}else if("orderState".equals(type)){
				sql+=" AND orderState.`orderStateId`=?";
			}
			sql+=" ORDER BY orderId DESC";
			list=jdbc.queryAll(sql, new RowMapperImpl(), id);
		}else{
			sql+=" ORDER BY orderId DESC";
			list=jdbc.queryAll(sql, new RowMapperImpl());
		}
		return list;
	}
	
	
}
